#include <myhead.h>
int main(int argc, const char *argv[])
{
	//1、
	int S=socket(AF_INET,SOCK_DGRAM,0);
	if(S==-1){perror("套接字创建失败\n");return -1;}
	//2、
	struct sockaddr_in addr={0};
	addr.sin_family=AF_INET;
	addr.sin_port=htons(12346);
	addr.sin_addr.s_addr=htonl(INADDR_ANY);
	if(bind(S,(struct sockaddr*)&addr,sizeof(addr))==-1){perror("服务器绑定失败");return -1;}
	printf("服务器绑定完成\n");
	/*
	//3、设置允许广播
	int m=1;
	setsockopt(S,SOL_SOCKET,SO_BROADCAST,&m,4);
	printf("广播配置完成\n");
	*/
	//组播地址
	struct sockaddr_in addt={0};
	addt.sin_family=AF_INET;
	addt.sin_port=htons(12345);
	addt.sin_addr.s_addr=inet_addr("224.0.0.1");

	//组播配置
	struct ip_mreqn addtz={0};
	addtz.imr_multiaddr.s_addr=inet_addr("224.0.0.1");
	addtz.imr_address.s_addr=htonl(INADDR_ANY);
	addtz.imr_ifindex=if_nametoindex("ens33");
	setsockopt(S,IPPROTO_IP,IP_MULTICAST_IF,&addtz,sizeof(addtz));
	unsigned short i;
	while(1)
	{
		i++;
		char str[20]={0};
		sprintf(str,"<%d>号GGBond",i);
		char buf[128]="你好,我是";
		strcat(buf,str);
		printf("开始广播:<%ld>%s\n",strlen(buf),buf);
		sendto(S,buf,strlen(buf),0,(struct sockaddr*)&addt,sizeof(addt));
		usleep(1000000);
	}

	return 0;
}
